package medium最小路径和;

import org.testng.annotations.Test;

public class Solution {
    public int minPathSum(int[][] grid) {
        int m = grid[0].length;
        int n = grid.length;
        int[][] num = new int[n][m];
        num[0][0] = grid[0][0];
        for (int i = 1; i < m; i++) {
            num[0][i] = num[0][i-1]+grid[0][i];
        }
        for (int i = 1; i < n; i++) {
            num[i][0] = num[i-1][0]+grid[i][0];
        }
        for (int i = 1; i < n; i++) {
            for (int j = 1; j < m; j++) {
                num[i][j] = Math.min(num[i-1][j],num[i][j-1])+grid[i][j];
            }
        }
//        for (int i = 0; i < n; i++) {
//            for (int j = 0; j < m; j++) {
//                System.out.print(num[i][j]+"   ");
//            }
//            System.out.println();
//        }
        return num[n-1][m-1];
    }
    @Test
    public void test(){
        int[][] num = {{1,3,1},{1,5,1},{4,2,1}};
        int[][] nums = {{1,2,3},{4,5,6}};
        minPathSum(nums);
    }
}
